import { Component, OnInit, ViewChild, EventEmitter } from '@angular/core';
import { Service } from '../../server/server';
import { DatabaseTableComponent } from '../database/database-table/database-table.component';
import { Pagination } from '../../public/serve/pcrudInfo';
import { DatabaseService } from './database.service';
import { HintsService } from 'app/public/serve/hints/hints.service';
import { CudTableService } from 'app/public/serve/cud-table/cud-table.service';
import { RemoveTableService } from 'app/public/serve/remove-table/remove-table.service';

declare var $:any;

@Component({
  selector: 'app-database',
  templateUrl: './database.component.html',
  styleUrls: ['./database.component.css']
})
export class DatabaseComponent implements OnInit {

  tableNumber : number = 0;
  right: any = { "system" : {}, "systemDatabase" : {} };
  constructor(
    private server:Service,
    private databaseService:DatabaseService,
    private hintsService: HintsService,
    private request: CudTableService,
    public removeTableService: RemoveTableService,
    ) { 
    this.right = $.extend(this.right, server.getRight());
  }

  public isActive= this.server.isActive;
  ngOnInit() {
    this.server.eventEmit.subscribe((value: any) => {
      this.isActive=value;
    });
    this.tableName = '';
  }

  //导入table
	@ViewChild("table")
  table: DatabaseTableComponent;
  // 删除按钮
  isSelect : boolean = true;
  isSelectFromTableHandler(isSelect: boolean) { 
    this.isSelect = isSelect;
  }

  // 禁用
  reduction() {
    setTimeout(()=>{
      this.isSelect = true;
    },100);
  }

  // 表格
	public totalItems: number;
	ParentList(totalItems: number) {
		this.totalItems = totalItems;
  }
  
  //查询
  public tableName:any;
  ParentSearch(val){
    //console.log(val);
    this.tableName = val;
    this.table.getTabList(this.pageNumber, this.pageSize,this.tableName);
  }

  //新增
  public newly_added_result_code: string;
  ParentAdd(result_code: string) {
    this.table.getTabList(this.pageNumber, this.pageSize, this.tableName);
  }
  
  public add(){
    this.databaseService.databaseAddeventEmit.emit(true);
  }
	
	// 修改
  public edit_result_code : string;
  ParentUpdate(result_code: string) {
    this.table.getTabList(this.pageNumber, this.pageSize, this.tableName);
	}

	//删除
  public remove_result_code: string;
  ParentDelete(result_code: string) {
    this.table.getTabList(this.pageNumber, this.pageSize, this.tableName);
  }

  // 当前页（默认为第一页）
	public pageNumber: number = 1;
	// 每页显示多少条数据（默认每页显示20条）
	public pageSize: number = 20;
	// id
	public id: string = '0';
	// 分页
	public pagination: Pagination;
	isLoadTableFromPaginationHandler(pagination: Pagination) {
		this.pagination = pagination;

		this.pageNumber = this.pagination[0].pageNumber;
		this.pageSize = this.pagination[0].pageSize;
		this.id = this.pagination[0].id;

		this.tableNumber = (pagination[0].pageNumber - 1) * pagination[0].pageSize;

		this.table.getTabList(this.pageNumber, this.pageSize, this.tableName);
  }
}
